package de.koller.worlders.engine.entity.mo.topdown;

import de.koller.oceanFramework.math.Vec;
import de.koller.oceanFramework.tree.Child;
import de.koller.worlders.engine.entity.IAABB;
import de.koller.worlders.engine.entity.mo.AbstractPlayer;
import de.koller.worlders.engine.util.Animation;
import de.koller.worlders.gameplay.IGameplayPlayerObject;
import de.koller.worlders.render.VisualMap;

public class TopDownPlayer extends AbstractPlayer implements IGameplayPlayerObject {

	@Child private final TDPlayerSteer playerSteer = new TDPlayerSteer();
	private float direction = 0;
	private float crosshairdis = 1;
	
	@Child private final Animation animation = new Animation( 
			VisualMap.tdPlayerStand, 
			VisualMap.tdPlayerWalk1,
			VisualMap.tdPlayerWalk2
		);
	private final int[] walkSequence = new int[] { 1, 0, 2, 0 };
	private final int[] standSequence = new int[] { 0 };
	
	public TopDownPlayer( float x, float y ) {
		super( x, y, .9f, .9f );
	}

	@Override
	protected void init() {
		super.init();

		this.setVisual( VisualMap.tdPlayerStand );
		this.mostate.setGravityAffected( false );
		this.mostate.setGameplayAffected( true );
		
		animation.setTick( 150 );
		
		this.reset();
	}

	@Override
	public boolean walk( float horizontal, float vertical ) {
		boolean b = super.walk( horizontal, vertical );
		
		if( horizontal == 0 && vertical == 0 )
			animation.animateSequenze( standSequence );
		else
			animation.animateSequenze( walkSequence );
		
		return b;
	}

	
	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		
		this.getEnergy().set( 0, 0 );
		this.getVelocity().set( 0, 0 );
		this.setVisualRotation( getScreenAngle() );
		
		Vec c = new Vec( crosshairdis, 0 );
		c.setDirection( -direction - Math.PI * .5f );
		c.add( this.getCentre() );
		wroot.getEntityContainer().getMouseWorldPosition().set( c );
	}
	
	@Override
	public IAABB getAABB() {
		return this;
	}

	@Override
	public float getScreenAngle() {
		return (float)Math.toDegrees( direction );
	}
	
	public float getDirection() {
		return direction;
	}

	public void setDirection( float direction ) {
		this.direction = direction;
	}

	public void setCrosshairdis( float crosshairdis ) {
		this.crosshairdis = crosshairdis;
		if( this.crosshairdis > 9 )
			this.crosshairdis = 9;
		if( this.crosshairdis <= 0 )
			this.crosshairdis = 0;
		
	}

	public float getCrosshairdis() {
		return crosshairdis;
	}
	
}
